C语言数组问题 a[-1]

来源:百度知道 编辑:UC知道 时间:2024/06/01 12:27:21
#include "stdio.h"
#include "string.h"
#define ERROR 0
#define OK 1

struct STU
{ char name[20];
char stuno[10];
int age; int score;
}stu[50];
struct LIST
{ struct STU stu[50];
int length;
}L;

int printlist(struct LIST L)
{ int i;
printf("name stuno age score\n");
for(i=0;i<L.length;i++)
printf("%s %s\t%d\t%d\n", L.stu[i].name, L.stu[i].stuno, L.stu[i].age, L.stu[i].score);
printf("\n");
return 1;
}

int listinsert(struct LIST *L,int i,struct STU e)
{ struct STU *p,*q;
if (i<1||i>L->length+1)
return ERROR;
q=&(L->stu[i-1]);
for(p=&L->stu[L->length-1];p>=q;--p)
*(p+1)=*p; *q=e; ++L->length;
return OK;
}/*ListInsert Before i */

int main()
{
struct STU e;
L.length=0;
strcpy(e.name,

因为前面有if (i<1||i>L->length+1) 进行条件判断。所以不会出现-1或0的情况

后面的for循环是向链表种插入元素用的。我想如果写成着这样你会明白一点
for(p=&L->stu[L->length-1];p>=q;--p)
{
*(p+1)=*p;
}
*q=e;
++L->length;

yes